#!/usr/bin/env node

const Koa = require('koa'),
      log = console.log;

const app = new Koa();
//用use加载中间件，固定参数，ctx上下文,next确保后面的中间件调用

// M-logger
app.use((ctx,next)=>{
  //解析请求
  log(`${ctx.method} ${ctx.path}`);
  //let cost = await next();
  //组装响应
  //let cost = ctx.response.get('cost');
  next();
  log(`cost:${ctx.cost}ms`);
});
app.use((ctx,next)=>{  //M-ms
  const start = Date.now()
  next();
  const end  = Date.now();
  //中间件传递数据
  //ctx.set('cost',end-start);  //响应头传数据
  //return end-start;  //return返回数据
  ctx.cost = end - start;
 
});
app.use((ctx,next)=>{   //M-hello
  //解析请求
  //封装响应
  ctx.body = 'hello koa';

});

app.listen(8080);

